{% extends "base.html" %}
{% block content_header %}
<h1>任务仪表盘</h1>
<ol style="float: right;
    margin-top: 0;
    margin-bottom: 0;
    font-size: 12px;
    padding: 7px 5px;
    position: absolute;
    top: 15px;
    right: 10px;">
    <button type="button" class="btn btn-success btn-flat" style="margin-top: -10px;" data-toggle="modal"
            data-target="#job-run-modal">运行一次
    </button>
</ol>
{% endblock %}
{% block content_body %}
<!--======next job======-->
<div class="box">
    <div class="box-header">
        <h3 class="box-title">下一个任务</h3>
        <div class="box-tools pull-right">
            <button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i>
            </button>
        </div>
    </div>
    <div class="box-body table-responsive">
        <table class="table table-striped">
            <tr>
                <th style="width: 10px">#</th>
                <th style="width: 30px">任务</th>
                <th style="width: 160px">爬虫蜘蛛</th>
                <th style="width: 100px">参数</th>
                <th style="width: 40px">优先级</th>
                <th style="width: 160px">延时</th>
            </tr>
            {% for job in job_status.PENDING %}
            {% if job.job_instance %}
            <tr>
                <td>{{ job.job_execution_id }}</td>
                <td><a href="/project/1/job/periodic#{{ job.job_instance_id }}">{{ job.job_instance_id }}</a></td>
                <td>{{ job.job_instance.spider_name }}</td>
                <td class="txt-args" data-toggle="tooltip" data-placement="right"
                    title="{{ job.job_instance.spider_arguments }}">{{ job.job_instance.spider_arguments }}
                </td>
                {% if job.job_instance.priority == -1 %}
                <td>
                    <span class="label label-default">低</span>
                </td>
                {% elif job.job_instance.priority == 0 %}
                <td>
                    <span class="label label-info">正常</span>
                </td>
                {% elif job.job_instance.priority == 1 %}
                <td>
                    <span class="label label-warning">高</span>
                </td>
                {% elif job.job_instance.priority == 2 %}
                <td>
                    <span class="label label-danger">最高</span>
                </td>
                {% endif %}
                <td>{{ timedelta(now,job.create_time) }}</td>
            </tr>
            {% endif %}
            {% endfor %}
        </table>
    </div>
</div>

<!--======running job======-->
<div class="box">
    <div class="box-header">
        <h3 class="box-title">运行任务</h3>
        <div class="box-tools pull-right">
            <button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i>
            </button>
        </div>
    </div>
    <div class="box-body table-responsive">
        <table class="table table-striped">
            <tr>
                <th style="width: 10px">#</th>
                <th style="width: 30px">任务</th>
                <th style="width: 160px">爬虫蜘蛛</th>
                <th style="width: 100px">参数</th>
                <th style="width: 20px">优先级</th>
                <th style="width: 40px">运行时间</th>
                <th style="width: 120px">启动时间</th>
                <th style="width: 40px">日志</th>
                <th style="width: 40px">运行服务器</th>
                <th style="width: 40px">操作</th>
            </tr>
            {% for job in job_status.RUNNING %}
            {% if job.job_instance %}
            <tr>
                <td>{{ job.job_execution_id }}</td>
                <td><a href="/project/1/job/periodic#{{ job.job_instance_id }}">{{ job.job_instance_id }}</a></td>
                <td>{{ job.job_instance.spider_name }}</td>
                <td class="txt-args" data-toggle="tooltip" data-placement="right"
                    title="{{ job.job_instance.spider_arguments }}">{{ job.job_instance.spider_arguments }}
                </td>
                {% if job.job_instance.priority == -1 %}
                <td>
                    <span class="label label-default">低</span>
                </td>
                {% elif job.job_instance.priority == 0 %}
                <td>
                    <span class="label label-info">正常</span>
                </td>
                {% elif job.job_instance.priority == 1 %}
                <td>
                    <span class="label label-warning">高</span>
                </td>
                {% elif job.job_instance.priority == 2 %}
                <td>
                    <span class="label label-danger">最高</span>
                </td>
                {% endif %}
                <td>{{ timedelta(now,job.start_time) }}</td>
                <td>{{ job.start_time }}</td>
                <td><a href="/project/{{ project.id }}/jobexecs/{{ job.job_execution_id }}/log" target="_blank"
                       data-toggle="tooltip" data-placement="top" title="{{ job.service_job_execution_id }}">日志</a>
                </td>
                <td style="font-size: 10px;">{{ job.running_on }}</td>
                <td>
                    <a href="/project/{{ project.id }}/jobexecs/{{ job.job_execution_id }}/stop"><span
                            class="label label-danger">停止</span></a>
                </td>
            </tr>
            {% endif %}
            {% endfor %}
        </table>
    </div>
</div>

<!--======completed job======-->
<div class="box">
    <div class="box-header">
        <h3 class="box-title">已完成任务</h3>
        <div class="box-tools pull-right">
            <button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i>
            </button>
        </div>
    </div>
    <div class="box-body table-responsive">
        <table class="table table-striped">
            <tr>
                <th style="width: 10px">#</th>
                <th style="width: 30px">任务</th>
                <th style="width: 160px">爬虫蜘蛛</th>
                <th style="width: 100px">参数</th>
                <th style="width: 20px">优先级</th>
                <th style="width: 40px">运行时间</th>
                <th style="width: 120px">启动时间</th>
                <th style="width: 10px">日志</th>
                <th style="width: 10px">状态</th>
            </tr>
            {% for job in job_status.COMPLETED %}
            {% if job.job_instance %}
            <tr>
                <td>{{ job.job_execution_id }}</td>
                <td><a href="/project/1/job/periodic#{{ job.job_instance_id }}">{{ job.job_instance_id }}</a></td>
                <td>{{ job.job_instance.spider_name }}</td>
                <td class="txt-args" data-toggle="tooltip" data-placement="right"
                    title="{{ job.job_instance.spider_arguments }}">{{ job.job_instance.spider_arguments }}
                </td>
                {% if job.job_instance.priority == -1 %}
                <td>
                    <span class="label label-default">低</span>
                </td>
                {% elif job.job_instance.priority == 0 %}
                <td>
                    <span class="label label-info">正常</span>
                </td>
                {% elif job.job_instance.priority == 1 %}
                <td>
                    <span class="label label-warning">高</span>
                </td>
                {% elif job.job_instance.priority == 2 %}
                <td>
                    <span class="label label-danger">最高</span>
                </td>
                {% endif %}
                <td>{{ timedelta(job.end_time,job.start_time) }}</td>
                <td>{{ job.start_time }}</td>
                <td><a href="/project/{{ project.id }}/jobexecs/{{ job.job_execution_id }}/log" target="_blank"
                       data-toggle="tooltip" data-placement="top" title="{{ job.service_job_execution_id }}">Log</a>
                </td>
                {% if job.running_status == 2 %}
                <td>
                    <span class="label label-success">完成</span>
                </td>
                {% else %}
                <td>
                    <span class="label label-danger">取消</span>
                </td>
                {% endif %}
            </tr>
            {% endif %}
            {% endfor %}
        </table>
    </div>
</div>

<div class="modal fade" role="dialog" id="job-run-modal">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <form action="/project/{{ project.id }}/job/add" method="post">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">×</span></button>
                    <h4 class="modal-title">爬虫启动一次</h4>
                </div>
                <div class="modal-body">
                    <div class="form-group">
                        <label>爬虫蜘蛛</label>
                        <select class="form-control" name="spider_name">
                            {% for spider in spider_list %}
                            <option value="{{ spider.spider_name }}">{{ spider.spider_name }}</option>
                            {% endfor %}
                        </select>
                    </div>
                    <div class="form-group">
                        <label>优先级</label>
                        <select class="form-control" name="priority">
                            <option label="Low" value="number:-1">低</option>
                            <option label="Normal" value="0" selected="selected">正常</option>
                            <option label="High" value="1">高</option>
                            <option label="Highest" value="2">最高</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label>参数</label>
                        <input type="text" name="spider_arguments" class="form-control" placeholder="foo=1,bar=2">
                    </div>
                    <div class="form-group">
                        <label>选择守护进程</label>
                        <select class="form-control" name="daemon">
                            <option value="auto">自动</option>
                            {% for server in servers %}
                            <option value="{{ server }}">{{ server }}</option>
                            {% endfor %}
                        </select>
                    </div>
                    <input type="hidden" name="run_type" value="onetime">
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default pull-left" data-dismiss="modal">关闭</button>
                    <button type="submit" class="btn btn-primary">创建</button>
                </div>
            </form>
        </div>
        <!-- /.modal-content -->
    </div>
    <!-- /.modal-dialog -->
</div>
<!-- /.modal -->
{% endblock %}